package com.own.business.system.account.controller.user;

import com.own.business.system.account.business.service.SystemAccountService;
import com.own.business.system.account.common.em.AccountType;
import com.own.business.system.account.controller.user.entity.form.LogoutForm;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.exception.BusinessException;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserSystemAccountController
 *
 * @author chenxueli
 * @date 2023-08-09 15:15:00
 */
@Tag(name = "系统账号" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "user/system/account")
public class UserSystemAccountController extends BaseController {

    @Resource
    private SystemAccountService systemAccountService;

    @LogOperationAop(operationName = "注销账号")
    @Operation(summary = "注销账号")
    @PostMapping("/logout")
    public ResultModel<Boolean> logout(
            @RequestBody LogoutForm form
    ) {
        if (StringUtils.isNotBlank(form.getReason()) && form.getReason().length() > 500) {
            throw new BusinessException("注销原因不能超过500个字符");
        }
        var loginUser = SessionUserUtil.getLoginUser();
        systemAccountService.logout(loginUser.userId(), form.getReason());
        return R.success(true, "注销成功");
    }

    @LogOperationAop
    @Operation(summary = "是否有密码")
    @PostMapping("/has/password")
    public ResultModel<Boolean> hasPassword() {
        var loginUser = SessionUserUtil.getLoginUser();
        var flag = systemAccountService.hasPassword(loginUser.userId(), AccountType.TEXT);
        return R.success(flag);
    }

}
